package com.pj.lib.aura;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.pj.aura.R;

public class MyLocationOverlay extends com.google.android.maps.MyLocationOverlay{

	static final int EQUATOR_LENGTH = 40075000;
	
	MapView mv;
	Context context;
	Boolean accuracyVisible;
	public MyLocationOverlay(Context context, MapView mapView) {
		
		super(context, mapView);
		mv = mapView;
		this.context = context;
		accuracyVisible = context.getResources().getBoolean(R.bool.lib_show_accuracy);
	}

	@Override
	public synchronized void onLocationChanged(Location location) {
		super.onLocationChanged(location);
		
	}

	@Override
	protected void drawMyLocation(Canvas canvas, MapView mapView,
			Location lastFix, GeoPoint myLocation, long when) {
/*		super.drawMyLocation(canvas, mapView, lastFix, myLocation, when);*/
		super.draw(canvas, mapView, true);
		Paint paint = new Paint();
	
		
		Point myScreenCoords = new Point();
		mv.getProjection().toPixels(myLocation, myScreenCoords);
		
		

		Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.lib_my_loc);
		
		canvas.drawBitmap(bmp, myScreenCoords.x - (float)(bmp.getWidth() /2), myScreenCoords.y - bmp.getHeight(), paint);
		if(accuracyVisible){
			paint = getAccuracyCirclePaint(paint);

			
			int zoomLevel = mv.getZoomLevel();
			int equatorInPixels = (int) Math.pow(2, zoomLevel) * 256;
			float accuracyInPixels = (float)((float) ( equatorInPixels * lastFix.getAccuracy()) / EQUATOR_LENGTH); 
			
			canvas.drawCircle(myScreenCoords.x, myScreenCoords.y, accuracyInPixels, paint);
		}
	}
	
	
	private Paint getAccuracyCirclePaint(Paint out) {
		out = new Paint();
	
		out.setColor(context.getResources().getColor(R.color.lib_paint_accuracy_stroke));
		out.setStyle(Paint.Style.FILL_AND_STROKE);
		out.setStrokeWidth(1);
		return out;
	}
	public void setAccuracyVisible(Boolean accuracyVisible) {
		this.accuracyVisible = accuracyVisible;
	}
	public boolean getAccuracyVisible() {
		return accuracyVisible;
	}
	
}
